import {  Modal} from 'antd';
import qs from "qs";
let pub_get = (url,data,headers)=>{
    return new Promise ((resolve,reject)=>{
        let str = "";
        for(var key in data){
            str += "&"+key+"="+data[key];
        }
        url = url+str.substr(1);
        const options = {
            method: 'GET',
            mode:'cors',
            headers: {
            ...headers
            },
        };
      fetch(url, {
            ...options,
        }).then(res=>res.json()).then(result=>{
            if(result.code == 2001){
                Modal.info({
                    title: '提示信息',
                    content:"登陆过期,请重新登陆",
                    onOk() {
                        window.localStorage.setItem("token",null);
                        window.location.href=('/')
                    },
                });
                return ;
            }
            if(result.code == 500){
                Modal.info({
                    title: '提示信息',
                    content:"内部错误",
                    onOk() {
                    },
                });
                return ;
            }
            resolve(result)
        }).catch(error => {
            console.info('  login error', error);
        });
    })
}

const pub_post = (url,method, data, headers) => {
    return new Promise ((resolve,reject)=>{
        let base_headers = {
            'Accept': 'application/json, text/plain, */*',
            'Content-Type': 'application/json',
        }
        switch(method){
            case "delete":
                base_headers = headers
               break;
            default:
               Object.assign(base_headers, headers)
               break;
        }
        return fetch(url, {
            method: method.toUpperCase(),
            headers: base_headers,
            body: JSON.stringify(data),
        }).then(res=>res.json()).then(result=>{
            if(result.code == 2001){
                Modal.info({
                    title: '提示信息',
                    content:"登陆过期,请重新登陆",
                    onOk() {
                        window.localStorage.setItem("token",null);
                        window.location.href=('/')
                    },
                });
                return ;
            }
            if(result.code == 500){
                Modal.info({
                    title: '提示信息',
                    content:"内部错误",
                    onOk() {
                    },
                });
                return ;
            }
            resolve(result)
        })
    })
}
export  {
    pub_get,
    pub_post,
}
